package com.xhb.ui.tools;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.sdk.service.cloudphone20201230.AsyncClient;
import com.aliyun.sdk.service.cloudphone20201230.models.SendFileRequest;
import com.aliyun.sdk.service.cloudphone20201230.models.SendFileResponse;
import com.google.gson.Gson;
import com.teamdev.jxbrowser.deps.com.google.common.collect.Lists;

import cn.hutool.core.io.FileUtil;
import darabonba.core.client.ClientOverrideConfiguration;

public class OSSUtil {
	
	public static String APP_KEY="LTAI5t7S2NwdKnJ2MK2F98td";
	
	public static String APP_SERCET="aSK4wi2opUljqGvvZtAEy642dh27gq";
	
	public static String REGIN="region";
	
	public static String END_POINT="cloudphone.cn-hangzhou.aliyuncs.com";
	
	public static String IMAGE_ID="cpm-bp1081yeklo2quujrvp1";

	
	public static void uploadCloudPhone(String dir, String cloudphoneCode) throws Exception {

		File fileUploadDir = new File(dir + "/hbbmp");

		if (!fileUploadDir.exists()) {
			return;
		}

		List<File> fileUploads = SortFileUtil.sort(fileUploadDir.getPath());

		if (fileUploads.size() == 0) {
			return;
		}

		File fileUpload = fileUploads.get(0);

		String endpoint = "oss-cn-hangzhou.aliyuncs.com";
		String accessKeyId = "LTAI5tKRbaqLrvRzGqJCEcbL";
		String accessKeySecret = "6PkXhLCztG70Y6MPvjBUIGlcmBmXAO";
		String bucketName = "jwebmall";

		// 创建OSS实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

		// 获取上传文件输入流
		InputStream inputStream = FileUtil.getInputStream(fileUpload);
		// 获取文件名称
		String fileName = "1000.bmp";
		// 2 把文件按照日期进行分类
		// 获取当前日期,格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		String datePath = sdf.format(new Date());
		// 拼接
		fileName = datePath + "/" + fileName;

		// end
		// 调用oss方法实现上传
		// 第一个参数 Bucket名称
		// 第二个参数 上传到oss文件路径和文件名称
		// 第三个参数 上传文件输入流
		ossClient.putObject(bucketName, fileName, inputStream);
		// 关闭OSSClient。
		ossClient.shutdown();

		// 把上传之后文件路径返回，需要把上传到阿里云oss路径手动拼接出来
		String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
		

		System.out.println(url);

		// Configure Credentials authentication information, including ak, secret, token
		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
				// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and
				// ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
				.accessKeyId(APP_KEY).accessKeySecret(APP_SERCET).build());

		// Configure the Client
		AsyncClient client = AsyncClient.builder().region(REGIN) // Region ID
				// .httpClient(httpClient) // Use the configured HttpClient, otherwise use the
				// default HttpClient (Apache HttpClient)
				.credentialsProvider(provider)
				// .serviceConfiguration(Configuration.create()) // Service-level configuration
				// Client-level configuration rewrite, can set Endpoint, Http request
				// parameters, etc.
				.overrideConfiguration(ClientOverrideConfiguration.create()
						// Endpoint 请参考 https://api.aliyun.com/product/cloudphone
						.setEndpointOverride(END_POINT)
				// .setConnectTimeout(Duration.ofSeconds(30))
				).build();

		List<String> instanceIdList = new ArrayList<String>();
		instanceIdList.add(cloudphoneCode);
		// Parameter settings for API request
		SendFileRequest sendFileRequest = SendFileRequest.builder().regionId(REGIN).instanceId(instanceIdList)
				.path("/storage/emulated/0/DCIM/Camera1").ossBucket(bucketName).ossObject(fileName)
				// Request-level configuration rewrite, can set Http request parameters, etc.
				// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new
				// HttpHeaders()))
				.build();

		// Asynchronously get the return value of the API request
		CompletableFuture<SendFileResponse> sendFileResponse = client.sendFile(sendFileRequest);
		// Synchronously get the return value of the API request
		SendFileResponse resp = sendFileResponse.get();

		client.close();
		
		{
			fileUpload.delete();
		}
		System.out.println(new Gson().toJson(resp));
	}
	
	
	public static void main(String [] args) throws Exception {
		//女
		OSSUtil.uploadCloudPhone("C:\\zgymw\\浙江学员名单\\庄千军-市政工程质量员-330326198310140758", "cp-bp1c9dtvmqyfuyrobws2");
		//男
		//OSSUtil.uploadCloudPhone("C:\\zgymw\\浙江学员名单\\庄千军-市政工程施工员-330326198310140758", "cp-bp14e0ai9283auiwolg9");
	}
}
